using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace CustomControls.UserControls
{
    public partial class UserControlColumnColors : UserControlBase
    {
        ColorDialog _colorDialog1;
        FontDialog _fontDialog1;

        public UserControlColumnColors()
        {
            InitializeComponent();
        }

        public override void Initialize()
        {
            _colorDialog1 = new ColorDialog();

            _colorDialog1.AllowFullOpen = true;
            _colorDialog1.AnyColor = true;
            _colorDialog1.FullOpen = true;
            _colorDialog1.SolidColorOnly = true;

            _colorDialog1.ShowHelp = false;

            _fontDialog1 = new FontDialog();

            _fontDialog1.ShowApply = false;
            _fontDialog1.ShowEffects = true;
            _fontDialog1.AllowScriptChange = true;
            _fontDialog1.AllowVerticalFonts = false;

            _fontDialog1.ScriptsOnly = false;
            _fontDialog1.FixedPitchOnly = false;

            _fontDialog1.AllowSimulations = true;
            _fontDialog1.AllowVectorFonts = true;
            _fontDialog1.FontMustExist = true;
            _fontDialog1.ShowColor = true;
            _fontDialog1.MaxSize = 10;

            //_configSettings = new ApplicationSettings();

            #region ** Column Colors  **

            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            buttonColumnNameColor1.BackColor = localSettings.ColumnSettings.PlainSettings.NameColors.Color1;// PlainColumnNameColor1;
            buttonColumnNameColor2.BackColor = localSettings.ColumnSettings.PlainSettings.NameColors.Color2;// PlainColumnNameColor2;
            spinnerColumnNameAngle.Value = (decimal)localSettings.ColumnSettings.PlainSettings.NameColors.Direction;// PlainColumnNameColorDirection;

            buttonColumnSizeColor1.BackColor = localSettings.ColumnSettings.PlainSettings.SizeColors.Color1;// PlainColumnSizeColor1;
            buttonColumnSizeColor2.BackColor = localSettings.ColumnSettings.PlainSettings.SizeColors.Color2;// PlainColumnSizeColor2;
            spinnerColumnSizeAngle.Value = (decimal)localSettings.ColumnSettings.PlainSettings.SizeColors.Direction;// PlainColumnSizeColorDirection;

            buttonColumnDateColor1.BackColor = localSettings.ColumnSettings.PlainSettings.DateColors.Color1;// PlainColumnDateColor1;
            buttonColumnDateColor2.BackColor = localSettings.ColumnSettings.PlainSettings.DateColors.Color2;// PlainColumnDateColor2;
            spinnerColumnDateAngle.Value = (decimal)localSettings.ColumnSettings.PlainSettings.DateColors.Direction;// PlainColumnDateColorDirection;

            #endregion

            #region ** Coloumn Highlight Colors **

            buttonColumnNameHighlightColor1.BackColor = localSettings.ColumnSettings.HighlightSettings.NameColors.Color1;// HighlightColumnNameColor1;
            buttonColumnNameHighlightColor2.BackColor = localSettings.ColumnSettings.HighlightSettings.NameColors.Color2;// HighlightColumnNameColor2;
            spinnerColumnNameHighlightAngle.Value = (decimal)localSettings.ColumnSettings.HighlightSettings.NameColors.Direction;// HighlightColumnNameColorDirection;

            buttonColumnSizeHighlightColor1.BackColor = localSettings.ColumnSettings.HighlightSettings.SizeColors.Color1;// HighlightColumnSizeColor1;
            buttonColumnSizeHighlightColor2.BackColor = localSettings.ColumnSettings.HighlightSettings.SizeColors.Color2;// HighlightColumnSizeColor2;
            spinnerColumnSizeHighlightAngle.Value = (decimal)localSettings.ColumnSettings.HighlightSettings.SizeColors.Direction;// HighlightColumnSizeColorDirection;

            buttonColumnDateHighlightColor1.BackColor = localSettings.ColumnSettings.HighlightSettings.DateColors.Color1;// HighlightColumnDateColor1;
            buttonColumnDateHighlightColor2.BackColor = localSettings.ColumnSettings.HighlightSettings.DateColors.Color2;// HighlightColumnDateColor2;
            spinnerColumnDateHighlightAngle.Value = (decimal)localSettings.ColumnSettings.HighlightSettings.DateColors.Direction;// HighlightColumnDateColorDirection;

            #endregion

            #region ** Fonts **

            labelColumnFont.Font = localSettings.ColumnSettings.PlainSettings.Font;// PlainColumnFont;
            labelColumnFont.ForeColor = localSettings.ColumnSettings.PlainSettings.FontColor;// PlainColumnForeColor;
            labelColumnFont.Text = localSettings.ColumnSettings.PlainSettings.Font.Name + @", " + localSettings.ColumnSettings.PlainSettings.Font.Size.ToString();

            #endregion

            PaintSampleBoxes();

            DirtyFlag = false;
        }


        private void buttonColor_Click(object argSender, EventArgs argEventArgs)
        {
            Button currentButton = (Button)argSender;

            List<int> customColors = new List<int>();

            //Add any custom colors.
            foreach (Control currentControl in tableLayoutPanelNameColors.Controls)
            {
                if (currentControl is DragColorButton)
                {
                    customColors.Add(CorrectedColor.CorrectColor(currentControl.BackColor));
                }
            }

            _colorDialog1.CustomColors = customColors.ToArray();

            _colorDialog1.Color = currentButton.BackColor;

            _colorDialog1.ShowDialog(this);

            currentButton.BackColor = _colorDialog1.Color;

            PaintSampleBoxes();

        }

        private void numericUpDownAngle_ValueChanged(object argSender, EventArgs argEventArgs)
        {
            //NumericUpDown currentControl = (NumericUpDown)argSender;

            PaintSampleBoxes();
        }

        private void buttonColumnFont_Click(object argSender, EventArgs argEventArgs)
        {
            _fontDialog1.ShowApply = false;
            _fontDialog1.ShowEffects = true;
            _fontDialog1.AllowScriptChange = true;
            _fontDialog1.AllowVerticalFonts = false;

            _fontDialog1.ScriptsOnly = false;
            _fontDialog1.FixedPitchOnly = false;

            _fontDialog1.AllowSimulations = true;
            _fontDialog1.AllowVectorFonts = true;
            _fontDialog1.FontMustExist = true;
            _fontDialog1.ShowColor = true;
            _fontDialog1.MaxSize = 10;

            _fontDialog1.Font = labelColumnFont.Font;
            _fontDialog1.Color = labelColumnFont.ForeColor;

            _fontDialog1.ShowDialog();

            Font currentFont = _fontDialog1.Font;
            Color currentFontColor = _fontDialog1.Color;

            labelColumnFont.Font = currentFont;
            labelColumnFont.ForeColor = currentFontColor;
            labelColumnFont.Text = currentFont.Name + @", " + currentFont.Size.ToString();

            DirtyFlag = true;
        }

        private void PaintSampleBoxes()
        {
            #region ** Column Header Colors **

            //Name Column
            float sampleGradientAngle = (float)spinnerColumnNameAngle.Value;

            if (sampleGradientAngle <= 0.0001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnNameSample.BackgroundImage = CreateBitmap(buttonColumnNameColor1.BackColor, buttonColumnNameColor2.BackColor, sampleGradientAngle);

            //Size Column
            sampleGradientAngle = (float)spinnerColumnSizeAngle.Value;

            if (sampleGradientAngle <= 0.00001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnSizeSample.BackgroundImage = CreateBitmap(buttonColumnSizeColor1.BackColor, buttonColumnSizeColor2.BackColor, sampleGradientAngle);

            //Date Column
            sampleGradientAngle = (float)spinnerColumnDateAngle.Value;

            if (sampleGradientAngle <= 0.00001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnDateSample.BackgroundImage = CreateBitmap(buttonColumnDateColor1.BackColor, buttonColumnDateColor2.BackColor, sampleGradientAngle);

            #endregion

            #region ** Column Header Highlight Colors **

            //Name Column
            sampleGradientAngle = (float)spinnerColumnNameHighlightAngle.Value;

            if (sampleGradientAngle <= 0.00001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnNameHighlightSample.BackgroundImage = CreateBitmap(buttonColumnNameHighlightColor1.BackColor, buttonColumnNameHighlightColor2.BackColor, sampleGradientAngle);

            //Size Column
            sampleGradientAngle = (float)spinnerColumnSizeHighlightAngle.Value;

            if (sampleGradientAngle <= 0.00001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnSizeHighlightSample.BackgroundImage = CreateBitmap(buttonColumnSizeHighlightColor1.BackColor, buttonColumnSizeHighlightColor2.BackColor, sampleGradientAngle);

            //Date Column
            sampleGradientAngle = (float)spinnerColumnDateHighlightAngle.Value;

            if (sampleGradientAngle <= 0.00001F)
                sampleGradientAngle = 1.00F;
            if (sampleGradientAngle >= 179.9999F)
                sampleGradientAngle = 179.00F;

            pictureBoxColumnDateHighlightSample.BackgroundImage = CreateBitmap(buttonColumnDateHighlightColor1.BackColor, buttonColumnDateHighlightColor2.BackColor, sampleGradientAngle);

            #endregion

            DirtyFlag = true;
        }

        private Bitmap CreateBitmap(Color argColor1, Color argColor2, float argSampleGradientAngle)
        {
            //Name
            Rectangle sampleRectangle = new Rectangle(0, 0, 100, 20);
            Bitmap sampleBitmap = new Bitmap(100, 20);
            Graphics sampleGraphic = Graphics.FromImage(sampleBitmap);

            LinearGradientBrush sampleBrush = new LinearGradientBrush(sampleRectangle, argColor1, argColor2, argSampleGradientAngle);

            sampleGraphic.FillRectangle(sampleBrush, 0, 0, sampleBitmap.Width, sampleBitmap.Height);
            return sampleBitmap;
        }

        public override void OnApply() //object sender, EventArgs argEventArgs)
        {

            if (!DirtyFlag)
            {
                return;
            }

            #region ** Column Header Colors **

            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Name Nodes
            localSettings.ColumnSettings.PlainSettings.NameColors.Color1 = buttonColumnNameColor1.BackColor;
            localSettings.ColumnSettings.PlainSettings.NameColors.Color2 = buttonColumnNameColor2.BackColor;
            localSettings.ColumnSettings.PlainSettings.NameColors.Direction = (float)spinnerColumnNameAngle.Value;

            //Size Nodes
            localSettings.ColumnSettings.PlainSettings.SizeColors.Color1 = buttonColumnSizeColor1.BackColor;
            localSettings.ColumnSettings.PlainSettings.SizeColors.Color2 = buttonColumnSizeColor2.BackColor;
            localSettings.ColumnSettings.PlainSettings.SizeColors.Direction = (float)spinnerColumnSizeAngle.Value;

            //Date Nodes
            localSettings.ColumnSettings.PlainSettings.DateColors.Color1 = buttonColumnDateColor1.BackColor;
            localSettings.ColumnSettings.PlainSettings.DateColors.Color2 = buttonColumnDateColor2.BackColor;
            localSettings.ColumnSettings.PlainSettings.DateColors.Direction = (float)spinnerColumnDateAngle.Value;

            #endregion

            #region ** Column Header Highlight Colors **

            //Name Nodes
            localSettings.ColumnSettings.HighlightSettings.NameColors.Color1 = buttonColumnNameHighlightColor1.BackColor;
            localSettings.ColumnSettings.HighlightSettings.NameColors.Color2 = buttonColumnNameHighlightColor2.BackColor;
            localSettings.ColumnSettings.HighlightSettings.NameColors.Direction = (float)spinnerColumnNameHighlightAngle.Value;

            ////Size Nodes
            localSettings.ColumnSettings.HighlightSettings.SizeColors.Color1 = buttonColumnSizeHighlightColor1.BackColor;
            localSettings.ColumnSettings.HighlightSettings.SizeColors.Color2 = buttonColumnSizeHighlightColor2.BackColor;
            localSettings.ColumnSettings.HighlightSettings.SizeColors.Direction = (float)spinnerColumnSizeHighlightAngle.Value;

            ////Date Nodes
            localSettings.ColumnSettings.HighlightSettings.DateColors.Color1 = buttonColumnDateHighlightColor1.BackColor;
            localSettings.ColumnSettings.HighlightSettings.DateColors.Color2 = buttonColumnDateHighlightColor2.BackColor;
            localSettings.ColumnSettings.HighlightSettings.DateColors.Direction = (float)spinnerColumnDateHighlightAngle.Value;

            #endregion

            #region ** Font info **

            localSettings.ColumnSettings.PlainSettings.Font = labelColumnFont.Font;
            localSettings.ColumnSettings.PlainSettings.FontColor = labelColumnFont.ForeColor;

            #endregion

            localSettings.Save();
            DirtyFlag = false;
        }

        private void button_BackColorChanged(object argSender, EventArgs argEventArgs)
        {
            Control currentControl = (Control)argSender;
            string toolTipString = String.Format("[ R={0}, G={1}, B={2}]", currentControl.BackColor.R, currentControl.BackColor.G, currentControl.BackColor.B);
            toolTip1.SetToolTip(currentControl, toolTipString);

            PaintSampleBoxes();
        }

    }
}
